package odfconverter;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileFilter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.text.DecimalFormat;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import org.xml.sax.SAXException;
import org.xml.sax.SAXNotRecognizedException;
import org.xml.sax.SAXNotSupportedException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;

public class XsltDemo {

	public static void main(String[] args) throws Exception {
		String toTestDir="";
		File xmlFile = null;
		File xslFile = null;
		xmlFile = new File("xsl/odf2oox/presentation/source.xml");// 任意的一个xml文件，仅作输入参数，实际转换的文件在样式单中已经指定。
		xslFile = new File("xsl/odf2oox/presentation/odf2oox.xsl");// 总样式单，其中通过xsl:import导入其它样式单
		PrintStream old = System.out;
		ZipURIResolver zipUIRResolver;
		ZipArchiveWriter finalWriter ;
		OoxSpacesPostProcessor ooxSpacePostProcessor;
		OoxCharactersPostProcessor ooxCharactersPostProcessor;
		SAXParserFactory spf;
		SAXParser saxParser;
		String fileSource,fileDest,filtTempXml;
		
		if(args.length>0){
			toTestDir = args[0];
		}else{
			System.out.print("Usage: java XsltDemo PathName.\nFor example: java XsltDemo e:\\testcase\\odp.");
			System.exit(0);
		}
		

		File directory = new File(toTestDir);
		File[] files = directory.listFiles(new myFileFilter());
		for (int i = 0; i < files.length; i++) {
			fileSource=files[i].getAbsolutePath();
			fileDest = fileSource.substring(0,fileSource.length()-4)+"(byjava).pptx";
			filtTempXml = fileSource.substring(0,fileSource.length()-4)+".xml";
			System.out.println(i + ":" + files[i].getName() + ";;;"
					+ files[i].getAbsolutePath());

			zipUIRResolver = new ZipURIResolver(files[i].getAbsolutePath());

			transform(xmlFile, xslFile, zipUIRResolver, fileDest, filtTempXml);
			System.setOut(old);
			// File f=new File("e:\\work\\FTEngine\\a.xml");
			// InputStream in = new FileInputStream(f);

			// SaxHandler saxHandler = new SaxHandler(source);
			// saxParse(in, saxHandler);

			finalWriter = new ZipArchiveWriter(zipUIRResolver);
			ooxSpacePostProcessor = new OoxSpacesPostProcessor(finalWriter);
			ooxCharactersPostProcessor = new OoxCharactersPostProcessor(ooxSpacePostProcessor);
			//String filename = "a.xml";
			// String filename = "d:/test.xml";
			spf = SAXParserFactory.newInstance();
			spf.setNamespaceAware(true);
			spf.setFeature("http://xml.org/sax/features/namespace-prefixes",true);
			saxParser = spf.newSAXParser();
			// saxParser.parse(new File(filename), finalWriter);
			// saxParser.parse(new File(filename), ooxSpacePostProcessor);
			System.out.println("start parse");
			saxParser.parse(new File(filtTempXml), ooxCharactersPostProcessor);

			System.out.println("Done");
		}

	}

	public static void transform(File xmlFile, File xsltFile,
			ZipURIResolver zipUIRResolver, String fileDest, String filtTempXml) throws TransformerException,
			TransformerConfigurationException {

		// 为转换器工厂设置其 XSLT 实现
		System.setProperty("javax.xml.transform.TransformerFactory",
				"net.sf.saxon.TransformerFactoryImpl");

		// 创建转换器工厂
		TransformerFactory tfactory = TransformerFactory.newInstance();

		// 创建 XSL 转换器
		System.out.println("load xslt file");
		Transformer transformer = tfactory.newTransformer(new StreamSource(
				xsltFile));
		// xsl文档中的参数需指定
		transformer.setParameter("documentType", "Document");
		transformer.setParameter("generator",
				"OpenXML/ODF Translator Command Line Tool 4.0");
		transformer.setParameter("outputFile", fileDest);
		// 为xsl文档中的document()函数指定URIResolver
		transformer.setURIResolver(zipUIRResolver);

		// 1:输出到控制台. 将源 XML 文档转换输出到 System.out。
		// transformer.transform(new StreamSource(xmlFile), new
		// StreamResult(System.out));

		// 2:输出重定向到文件.
		System.out.println("start tranforming");
		try {
			System.setOut(new PrintStream(new BufferedOutputStream(
					new FileOutputStream(filtTempXml))));
		} catch (Exception e) {
			e.printStackTrace();
		}

		// 将源 XML 文档转换输出到 System.out。

		transformer.transform(new StreamSource(xmlFile), new StreamResult(
				System.out));
		System.out.println("tranforming is over");

	}
}

class myFileFilter implements FileFilter {
	@Override
	public boolean accept(File pathname) {
		String filename = pathname.getName().toLowerCase();
		if (filename.contains(".odp")) {
			if (filename.substring(filename.length() - 4).equals(".odp")) {
				return true;
			} else {
				return false;
			}
		} else {
			return false;
		}
	}
}